package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.pojo.TCar;
import com.dk.mapper.TCarMapper;
import com.dk.service.ITCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@Service
public class TCarServiceImpl extends ServiceImpl<TCarMapper, TCar> implements ITCarService {

    @Autowired
    private TCarMapper tCarMapper;

    @Autowired
    private RestHighLevelClient client;

    @Override
    public List<TCar> selectList(QueryWrapper wrapper) {
        return tCarMapper.selectList(wrapper);
    }

    //车名搜索加分页
    @Override
    public List<TCar> getInfoByName(String name, Integer startIndex, Integer pageSize) {
        //构建返回结果
        List<TCar> tCarList = new ArrayList<>();
        //构建searchRequest请求对象,指定索引库
        SearchRequest searchRequest = new SearchRequest("dongdongershouche1");
        //构建SearchSourceBuilder查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //带分析的查询
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", name);
        //分页
        searchSourceBuilder.from(startIndex);
        searchSourceBuilder.size(pageSize);
        //将QueryBuilder对象设置到SearchSourceBuilder中
        searchSourceBuilder.query(queryBuilder);
        //高亮显示,构建HighlightBuilder高亮对象
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //设置要高亮的字段
        highlightBuilder.field("name");
        //设置高亮样式
        highlightBuilder.preTags("<font color = 'red'>");
        highlightBuilder.postTags("</font>");
        //将高亮对象highlightBuilder设置到searchSourceBuilder中
        searchSourceBuilder.highlighter(highlightBuilder);
        //将SearchSourceBuilder对象封装到请求对象SearchRequest中
        searchRequest.source(searchSourceBuilder);
        //调用方法进行数据通信
        try {
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                String jsonString = hit.getSourceAsString();
                TCar tCar = JSONObject.parseObject(jsonString, TCar.class);
                //高亮显示
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                HighlightField title = highlightFields.get("name");
                Text[] fragments = title.getFragments();
                for (Text fragment : fragments) {
                    System.out.println(fragment.toString());
                    tCar.setName(fragment.toString());
                }
                //把car对象放入list集合中
                tCarList.add(tCar);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tCarList;
    }

    //根据标签查询es
    @Override
    public List<TCar> getInfoByTitle(String label) {
        //构建返回结果
        List<TCar> tCarList = new ArrayList<>();
        //构建searchRequest请求对象,指定索引库
        SearchRequest searchRequest = new SearchRequest("dongdongershouche1");
        //构建SearchSourceBuilder查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //带分析的查询
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("label", label);
        //将QueryBuilder对象设置到SearchSourceBuilder中
        searchSourceBuilder.query(queryBuilder);
        //高亮显示,构建HighlightBuilder高亮对象
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //设置要高亮的字段
        highlightBuilder.field("label");
        //设置高亮样式
        highlightBuilder.preTags("<font color = 'red'>");
        highlightBuilder.postTags("</font>");
        //将高亮对象highlightBuilder设置到searchSourceBuilder中
        searchSourceBuilder.highlighter(highlightBuilder);
        //将SearchSourceBuilder对象封装到请求对象SearchRequest中
        searchRequest.source(searchSourceBuilder);
        //调用方法进行数据通信
        try {
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                String jsonString = hit.getSourceAsString();
                TCar tCar = JSONObject.parseObject(jsonString, TCar.class);
                //高亮显示
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                HighlightField title = highlightFields.get("label");
                Text[] fragments = title.getFragments();
                for (Text fragment : fragments) {
                    System.out.println(fragment.toString());
                    tCar.setLabel(fragment.toString());
                }
                //把car对象放入list集合中
                tCarList.add(tCar);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tCarList;
    }

    //按上架时间查询分页
    @Override
    public List<TCar> getInfoByTime(Integer startIndex, Integer pageSize) {
        //构建返回结果
        List<TCar> tCarList = new ArrayList<>();
        //构建searchRequest请求对象,指定索引库
        SearchRequest searchRequest = new SearchRequest("dongdongershouche1");
        //构建SearchSourceBuilder查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //带分析的查询   按最新上架的查询
        FieldSortBuilder fieldSortBuilder = SortBuilders.fieldSort("create_time");
        fieldSortBuilder.order(SortOrder.DESC);
        //将QueryBuilder对象设置到SearchSourceBuilder中
        searchSourceBuilder.sort(fieldSortBuilder);
        //分页
        searchSourceBuilder.from(startIndex);
        searchSourceBuilder.size(pageSize);
        //将SearchSourceBuilder对象封装到请求对象SearchRequest中
        searchRequest.source(searchSourceBuilder);
        //调用方法进行数据通信
        try {
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                String jsonString = hit.getSourceAsString();
                TCar tCar = JSONObject.parseObject(jsonString, TCar.class);
                //把car对象放入list集合中
                tCarList.add(tCar);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tCarList;
    }
}
